/**
 * @(#)HttpCaller.java 2016年11月3日
 * Copyright(C) 2015 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.httpclient;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jsbd.boss.httpclient.vo.BaseRequest;
import org.jsbd.boss.httpclient.vo.ProxyInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 请求抽象类
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
public abstract class HttpCaller {
	public static final Logger LOGGER = LoggerFactory.getLogger(HttpCaller.class);

	private BaseRequest request;
	protected CloseableHttpClient httpclient;

	public HttpCaller(BaseRequest request) {
		this.request = request;
		httpclient = HttpClients.createDefault();
	}

	/**
	 * 
	 * buildRequestConfig:构建request请求配置信息
	 * 
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 * @param proxyInfo
	 * @param timeout
	 * @return
	 */
	protected RequestConfig buildRequestConfig() {
		ProxyInfo proxyInfo = request.getProxyInfo();
		int timeout = request.getTimeout();
		RequestConfig requestConfig = null;
		if (proxyInfo != null) {
			HttpHost host = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort());
			requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setProxy(host).build();
		} else {
			requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).build();
		}
		return requestConfig;
	}

}
